package com.youlai.boot.modules.delivery.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.modules.delivery.converter.DeliveryConverter;
import com.youlai.boot.modules.delivery.mapper.DeliveryMapper;
import com.youlai.boot.modules.delivery.model.Delivery;
import com.youlai.boot.modules.delivery.model.DeliveryForm;
import com.youlai.boot.modules.delivery.model.DeliveryVO;
import com.youlai.boot.modules.delivery.model.DeliveryPageQuery;
import com.youlai.boot.modules.delivery.service.DeliveryService;
import com.youlai.boot.modules.product.model.Product;
import com.youlai.boot.modules.product.mapper.ProductMapper;
import com.youlai.boot.common.exception.BusinessException;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class DeliveryServiceImpl extends ServiceImpl<DeliveryMapper, Delivery> implements DeliveryService {

    private final DeliveryConverter deliveryConverter;
    private final ProductMapper productMapper;
    private final DeliveryMapper deliveryMapper;

    @Override
    public Page<DeliveryVO> getDeliveryPage(DeliveryPageQuery query) {
        Page<DeliveryVO> page = new Page<>(query.getPageNum(), query.getPageSize());
        return baseMapper.getDeliveryPage(page, query);
    }

    @Override
    public DeliveryVO getDeliveryById(Long id) {
        Delivery delivery = this.getById(id);
        return deliveryConverter.toVO(delivery);
    }

    @Override
    public Long saveDelivery(DeliveryForm form) {
        Delivery delivery = deliveryConverter.toEntity(form);
        this.save(delivery);
        return delivery.getId();
    }

    @Override
    public Page<DeliveryVO> getProductDeliveryPage(Long productId, DeliveryPageQuery query) {
        Page<DeliveryVO> page = new Page<>(query.getPageNum(), query.getPageSize());
        return deliveryMapper.getProductDeliveryPage(page, productId);
    }

    @Override
    public void updateDelivery(DeliveryForm form) {
        Delivery delivery = deliveryConverter.toEntity(form);
        this.updateById(delivery);
    }

    @Override
    public boolean deleteDelivery(Long id) {
        return this.removeById(id);
    }

    @Override
    public boolean saveProductDelivery(Long productId, Long deliveryId) {
        // 检查商品是否存在
        Product product = productMapper.selectById(productId);
        if (product == null) {
            throw new BusinessException("商品不存在");
        }

        // 检查物流公司是否存在
        Delivery delivery = deliveryMapper.selectById(deliveryId);
        if (delivery == null) {
            throw new BusinessException("物流公司不存在");
        }

        // 检查商品是否已关联物流公司
        boolean exists = deliveryMapper.checkProductDeliveryExists(productId, deliveryId);
        if (exists) {
            throw new BusinessException("该商品已关联此物流公司");
        }

        return deliveryMapper.saveProductDelivery(productId, deliveryId);
    }

    @Override
    public boolean deleteProductDelivery(Long productId, Long deliveryId) {
        return deliveryMapper.deleteProductDelivery(productId, deliveryId);
    }    
}